package com.itita.ww2.handler.requesthandler.pay;

import java.sql.SQLException;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.itita.ww2.core.WW2Exception;
import com.itita.ww2.utils.handler.IRequestOper;

public class ApplePay implements IRequestOper {

	@Override
	public boolean execute(String requestCMD, User user, ISFSObject params) throws WW2Exception, SQLException {
		//		ISFSObject applePayConfig = ServerConfig.getGameConfig(R.CFG_KEY_PAYMENT).getSFSObject("applePay");
		//		/**
		//		 * iOS <= 6
		//		 * error,payment,transactionState,transactionIdentifier
		//		 * iOS 7+ 
		//		 * bundle_id
		//		 *
		//		 */
		//		// 打印出来看看
		//		ext.trace(params.toJson());
		//		
		//		ISFSObject fields = params.getSFSObject("json");
		//		
		//		ISFSObject sendParams = new SFSObject();
		//		sendParams.putUtfString("receipt-data", fields.getUtfString("json"));
		//		sendParams.putUtfString("password", applePayConfig.getUtfString("shared_secret"));
		//		
		//		
		//		
		//		String url = applePayConfig.getBool("isDebug") ? applePayConfig.getUtfString("receiptUrlTest") : applePayConfig.getUtfString("receiptUrl");
		//		HttpPost post = new HttpPost(url);
		//		post.addHeader("content-type", "application/x-www-form-urlencoded");
		//		
		//		// 构造body
		//		ArrayList<NameValuePair> postBody = new ArrayList<>();
		//		postBody.add(
		//				new BasicNameValuePair("receipt-data", fields.getUtfString("json")));
		//		postBody.add(new BasicNameValuePair("password", applePayConfig.getUtfString("shared_secret")));
		//
		//		// ext.trace(EntityUtils.toString(postEntity));
		//		post.setEntity(new UrlEncodedFormEntity(postBody, Consts.UTF_8));
		//		
		//		CloseableHttpAsyncClient httpClient = HttpUtil.getHttpAsyncClientNoop();
		//		
		//		httpClient.start();
		//		httpClient.execute(post, new FutureCallback<HttpResponse>() {
		//			ISFSObject backObj = new SFSObject();
		//			
		//			@Override
		//			public void failed(Exception arg0) {
		//				backObj.putUtfString(R.DATA_KEY_RESULT, "NO");// 默认为NO
		//				ext.send(requestCMD, backObj, user);
		//			}
		//			
		//			@Override
		//			public void completed(HttpResponse arg0) {
		//				String result = null;
		//				try {
		//					HttpEntity entity = arg0.getEntity();
		//					result = EntityUtils.toString(entity);
		//					EntityUtils.consume(entity);
		//				} catch (ParseException | IOException e) {
		//					e.printStackTrace();
		//				}
		//				
		//				ISFSObject response = SFSObject.newFromJsonData(result);
		//				
		////				int code = response.getInt("status"); // 0-success or error code 
		////				response.getUtfString("receipt");
		////				response.getUtfString("latest_receipt"); // only - ios6
		////				response.getUtfString("latest_receipt_into"); // only - ios6
		//				ext.trace(response.toJson());
		//				
		//				// 暂时回复失败
		//				backObj.putUtfString(R.DATA_KEY_RESULT, "NO");// 默认为NO
		//				ext.send(requestCMD, backObj, user);
		//			}
		//			
		//			@Override
		//			public void cancelled() {
		//				backObj.putUtfString(R.DATA_KEY_RESULT, "NO");// 默认为NO
		//				ext.send(requestCMD, backObj, user);
		//			}
		//		});
		return true;
	}

}
